
import 'dart:io';

import 'package:baselib/utils/gallery_camera_pick_util.dart';
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:flutter/src/widgets/editable_text.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/oss_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/interest_tag_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:module_business/module_index/model/index_room_item_entity.dart';
import 'package:module_business/module_index/model/voice_person_entity.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:nim_core/nim_core.dart';

import 'index_view_model.dart';
import 'my_room_view_model.dart';

class RoomSetViewModel extends BaseViewModel<ModuleIndexService>{

  ModuleIndexService moduleIndexService;
  IndexRoomItemEntity roomInfo;
  NIMChatroomInfo roomYxInfo;

  TextEditingController nameTextController = TextEditingController();
  TextEditingController introductionTextController = TextEditingController();
  TextEditingController passwordTextController = TextEditingController();

  RxBool isEnableCharmValue = false.obs;
  File coverFile;
  VoicePersonEntity voicePersonEntity = VoicePersonEntity();


  @override
  ModuleIndexService createService() =>moduleIndexService = ModuleIndexService();
  @override
  void onInit() {
    super.onInit();
    Map<String,dynamic> arguments = Get.arguments??{};
    roomInfo =  arguments['roomInfo'];
    roomYxInfo =  arguments['roomYxInfo'];
    initSetData();
    //查询此用户和此房间的关系
    queryUserRoomRelationType();
  }
  //查询此用户和此房间的关系
  void queryUserRoomRelationType() async{
    Map<String,dynamic> upData={
      "roomId": roomInfo?.id,
      "userId": UserParamsModel.instance.userId
    };
    BaseEntity baseEntity = await moduleIndexService.
    api_voicechat_room_queryUserRoomRelationType(parmsMap: upData);
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      voicePersonEntity = baseEntity?.result??VoicePersonEntity();
      update();
    }
  }
  //关系类型:2:管理员,3:主播,4:黑名单,6:禁言
  bool isHasPermission(){
    return (voicePersonEntity?.relationType == 1)||(voicePersonEntity?.relationType == 2);
  }
  initSetData(){
    LogUtil.d('initSetData--->roomInfo=${roomInfo?.toJson()}');
    nameTextController.text = roomInfo.roomName;
    introductionTextController.text = roomInfo.roomSynopsis;
    passwordTextController.text = roomInfo.password;


  }
  @override
  void onClose() {
    super.onClose();
    if(nameTextController!=null)nameTextController.dispose();
    if(introductionTextController!=null)introductionTextController.dispose();
    if(passwordTextController!=null)passwordTextController.dispose();
  }

  save() async{
    LoadUtil.showLoad(loadingText: StringUtil.instance.Updating);
    roomInfo.roomName = nameTextController.text;
    roomInfo.roomSynopsis = introductionTextController.text;
    roomInfo.password = passwordTextController.text;
    Map<String,dynamic> parmsMap = {
      "charismaShow": roomInfo?.charismaShow,
    "coverImg": roomInfo?.coverImg,
    "password": roomInfo?.password??'',
    "roomId": roomInfo?.id,
    "roomName": roomInfo?.roomName,
    "roomSynopsis": roomInfo?.roomSynopsis,
    // "roomThemeId": roomInfo?.id
    };
    BaseEntity baseEntity = await moduleIndexService.api_voicechat_room_updateChatRoom(parmsMap: parmsMap);
    LoadUtil.hideLoad();
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      ToastUtil.toast(StringUtil.instance.Update_success);
      //刷新
      IndexViewModel indexViewModel = Get.find();
      if(indexViewModel!=null)indexViewModel.getMyRoom();
      MyRoomViewModel myRoomViewModel = Get.find();
      if(myRoomViewModel!=null)myRoomViewModel.getRommInfo();

    }else{
      ToastUtil.toast(baseEntity?.message);
    }
  }

  setEnableCharmValue(bool isEnable) {
    if(!isHasPermission())return;
    isEnableCharmValue.value = isEnable;
    //是否开启魅力值显示.1:开启,0:关闭
    roomInfo?.charismaShow = (isEnableCharmValue.value??false)?1:0;
  }

  onCoverTap() async{
    coverFile = await GalleryCameraPickUtil.instance.selectPicture();
    if(coverFile==null)return;
    LoadUtil.showLoad(loadingText: '${StringUtil.instance.Uploading}...');
    OssUtil.instance.upload(coverFile, complete: (path,isUpload){
      LoadUtil.hideLoad();
      roomInfo?.coverImg = path;
      LogUtil.d('selectCover-->coverPath=${roomInfo?.coverImg}');
      update();
    });
  }

  toFuncPage(String tag) {
    String route = '';
    if(tag == StringUtil.instance.Release_notification)route = RouteConfig.roomReleaseNotificationPage;
    if(tag == StringUtil.instance.Admins)route = RouteConfig.roomAdminsPage;
    if(tag == StringUtil.instance.Blocklist)route = RouteConfig.roomBlocklistPage;
    if(tag == StringUtil.instance.Forbidden_List)route = RouteConfig.roomForbiddenListPage;
    if(route.isEmpty)return;
    NavigatorUtil.pushByName(route,arguments: {
      'roomYxInfo':roomYxInfo,
      'roomInfo':roomInfo,
    });
  }
}
